﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using ElementumForever.Interfaces;
using ElementumForever.Enums;
using System.ComponentModel;
using ElementumForever.Utility;
using ElementumForever.CommandPattern;
using Microsoft.Xna.Framework;

namespace ElementumForever.Editer
{
    public class EntityPropertyGridData : IEntityProperty
    {
        protected IEntityProperty entity;
        MainFormModel model = null;

        public EntityPropertyGridData()
        {
            Initialize();
        }

        public EntityPropertyGridData(IEntityProperty _propertyMaster, MainFormModel model)
        {
            this.entity = _propertyMaster;
            this.model = model;
        }

        protected virtual void Initialize()
        {
            if (entity == null)
                this.entity = new EntityPropertyProxy();
            IsOriginCenter = true;
        }

        bool isOriginCenter;

        public virtual bool IsOriginCenter
        {
            get
            {
                if (entity.OriginX == entity.Width / 2 && entity.OriginY == entity.Height / 2)
                    isOriginCenter = true;

                return isOriginCenter;
            }
            set
            {
                isOriginCenter = value;
                if (value)
                {
                    OriginX = Width / 2;
                    OriginY = Height / 2;
                }
            }
        }

        [Category("Size")]
        public float Height
        {
            get
            {
                return entity.Height;
            }
            set
            {
                entity.Height = value;
                if (IsOriginCenter)
                    OriginY = Height / 2;
            }
        }

        public float LayerDepth
        {
            get
            {
                return entity.LayerDepth;
            }
            set
            {
                entity.LayerDepth = value;
            }
        }

        public string Name
        {
            get
            {
                return entity.Name;
            }
            set
            {
                if (model != null)
                    model.AddCommand(new ChangeNameCommand(entity, entity.Name, value));
                else
                    entity.Name = value;
            }
        }

        public EObjectClass ObjectClass
        {
            get
            {
                return entity.ObjectClass;
            }
            set { entity.ObjectClass = value; }

        }

        [Category("Origin")]
        public float OriginX
        {
            get
            {
                return entity.OriginX;
            }
            set
            {
                if (IsOriginCenter)
                    entity.OriginX = Width / 2;
                else
                    entity.OriginX = value;
            }
        }

        [Category("Origin")]
        public float OriginY
        {
            get { return entity.OriginY; }
            set
            {
                if (IsOriginCenter)
                    entity.OriginY = Height / 2;
                else
                    entity.OriginY = value;
            }
        }

        public float Rotation//degree
        {
            get
            {
                return entity.Rotation;
            }
            set
            {
                if (model != null)
                    model.AddCommand(new ChangeRotationCommand(entity, entity.Rotation, value));
                else
                    entity.Rotation = value;
            }
        }

        [Category("Scale")]
        public float ScaleX
        {
            get { return entity.ScaleX; }
            set { entity.ScaleX = value; }
        }
        [Category("Scale")]
        public float ScaleY
        {
            get { return entity.ScaleY; }
            set { entity.ScaleY = value; }
        }

        [Category("Size")]
        public float Width
        {
            get { return entity.Width; }
            set
            {
                entity.Width = value;
                if (IsOriginCenter)
                    OriginX = Width / 2;
            }
        }

        [Category("Position")]
        public virtual float X
        {
            get { return entity.X; }
            set
            {
                if (model != null)
                    model.AddCommand(new ChangeXYCommand(entity, entity.X, value, entity.Y, entity.Y));
                else
                    entity.X = value;
            }
        }

        [Category("Position")]
        public virtual float Y
        {
            get { return entity.Y; }
            set
            {
                if (model != null)
                    model.AddCommand(new ChangeXYCommand(entity, entity.X, entity.X, entity.Y, value));
                else
                    entity.Y = value;
            }
        }

        public float Alpha
        {
            get { return entity.Alpha; }
            set { entity.Alpha = value; }
        }

        public bool Visiable
        {
            get { return entity.Visiable; }
            set { entity.Visiable = value; }
        }

        [Browsable(false)]
        public Microsoft.Xna.Framework.Color Color
        {
            get { return entity.Color; }
            set
            {
                entity.Color = value;
            }
        }

        public System.Drawing.Color FillColor
        {
            get
            {
                return UnitHelper.ToColorForm(entity.Color);
            }
            set
            {
                //fillColor = value;
                Color = UnitHelper.ToColorXna(value);
            }
        }

    }
}
